#region Copyright

// The contents of this file are subject to the Mozilla Public License
//  Version 1.1 (the "License"); you may not use this file except in compliance
//  with the License. You may obtain a copy of the License at
//  
//  http://www.mozilla.org/MPL/
//  
//  Software distributed under the License is distributed on an "AS IS"
//  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//  License for the specific language governing rights and limitations under 
//  the License.
//  
//  The Initial Developer of the Original Code is Robert Smyth.
//  Portions created by Robert Smyth are Copyright (C) 2008.
//  
//  All Rights Reserved.

#endregion

using System;
using NUnitGrid.Protocol.Commands;


namespace NUnitGrid.Protocol
{
    public class RunAsProcessTask : IRunOnGridTask, IGridTaskTransactionListener
    {
        private readonly string clientMachineName;
        private readonly string commandLine;
        private readonly string enviromentVariable;
        private readonly string enviromentVariableValue;
        private readonly string fileName;
        private readonly Guid sessionID;
        private readonly TimeSpan timeLimit;
        private readonly string workingDirectory;
        private IGridTaskTransactionListener listener;

        public RunAsProcessTask(string clientMachineName, string fileName, string workingDirectory, string commandLine,
                                TimeSpan timeLimit, string enviromentVariable, string enviromentVariableValue,
                                Guid sessionID)
        {
            this.fileName = fileName;
            this.workingDirectory = workingDirectory;
            this.commandLine = commandLine;
            this.timeLimit = timeLimit;
            this.enviromentVariable = enviromentVariable;
            this.enviromentVariableValue = enviromentVariableValue;
            this.sessionID = sessionID;
            this.clientMachineName = clientMachineName;
        }

        public void SendCommand(IGridTaskTransactionListener taskListener, IGridProtocol protocol)
        {
            listener = taskListener;
            protocol.RunAsProcess(this, fileName, clientMachineName, workingDirectory,
                                  commandLine, timeLimit, enviromentVariable, enviromentVariableValue, sessionID);
        }

        void IGridTaskTransactionListener.OnTaskStarted(object sender)
        {
            listener.OnTaskStarted(this);
        }

        void IGridTaskTransactionListener.OnTaskCompleted(object sender, bool succeeded, string textOutput,
                                                          string machineName)
        {
            listener.OnTaskCompleted(this, succeeded, textOutput, machineName);
        }
    }
}